﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class SearchPayment : Form
    {
        public String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NauferTransactionDemoDB.accdb";
        public SearchPayment()
        {
            InitializeComponent();
            showallPayment();
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            if (txtjobcode.Text.Trim() == null || txtjobcode.Text.Trim() == "")
            {
                if (txtvehicleno.Enabled == true)
                {
                    searchbyvehicleno();
                }
                else
                {
                    lblstatus.Text = " Enter a job code And Search";
                    txtjobcode.Focus();
                }
            }
            else
            {
                showallPaymentbyjobcode();

                if (tblpaymentview.RowCount == 0)
                {
                    lblstatus.Text = "No Payment Done";
                }


            }
        }


        public void showallPayment()
        {
            try
            {



                String query = "Select jobcode ,drivername,vehicleno,amount,postdate,description,ischeque,iscash from driverpaymentmastertable Order by postdate";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblpayview");
                tblpaymentview.DataSource = ds.Tables["tblpayview"].DefaultView;
                if (ds.Tables.Count <= 0)
                {
                    lblstatus.Text = "No Payment Details Present";
                }
            }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files");

            }


        }

        public void showallPaymentbyjobcode()
        {
            try
            {



                String query = "Select jobcode ,vehicleno,amount,postdate,description,ischeque,iscash from driverpaymentmastertable  where jobcode Like '" + txtjobcode.Text + "'  Order by postdate";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblpayview");
                tblpaymentview.DataSource = ds.Tables["tblpayview"].DefaultView;
                if (ds.Tables.Count <= 0)
                {
                    lblstatus.Text = "No Payment Details Present";
                }
            }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files");

            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }


        private void txtjobcode_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                txtjobcode.Enabled = false;
                txtvehicleno.Enabled = true;
                txtvehicleno.Focus();
            }
        }
        public void searchbyvehicleno()
        {

            try
            {
            OleDbConnection oledbconnection = new OleDbConnection(connString);
            oledbconnection.Open();
            
                String query = "Select jobcode ,drivername,amount,postdate,description,ischeque,iscash from driverpaymentmastertable where vehicleno='" + txtvehicleno.Text + "' Order by postdate";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet(); dAdapter.Fill(ds, "tbluserdataview");
                tblpaymentview.DataSource = ds.Tables["tbluserdataview"].DefaultView;
            }
            catch (Exception exp)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:close MSAccess files");
                Console.WriteLine("" + exp);
            }
          
         


        }


    }
}
